<?php

namespace plugin\muadmin\app\common\core;

class BaseDict
{
    protected static $instance;
    protected $data = [

    ];
    public function __construct($config=[])
    {
        if(!empty($config)){
            $this->data = $config;
        }
    }
    public static function getInstance($config=[])
    {
        if(!isset(self::$instance)){
            self::$instance = new static($config);
        }
        return self::$instance;
    }

    /**
     * 获取字典选项
     * @param $key
     * @param $default
     * @param $keyToValue
     * @return array|false|mixed|string
     */
    public function getOptions($key='',$default='',$keyToValue=false)
    {
        if(empty($this->data['option'])){
            return $default;
        }
        $options = $this->data['option'];
        if($keyToValue){
            $options = array_combine(array_column($options,'value'),array_column($options,'title'));
            if(empty($key)){
                return $options;
            }
            return isset($options[$key])?$options[$key]:$default;
        }else{
            if(empty($key)){
                return $options;
            }
            foreach ($options as $k=>$v){
                if($v['value']==$key){
                    return $v['title'];
                }
            }
            return $default;
        }
    }

    /**
     * 获取字典
     * @return array|mixed
     */
    public function getDict()
    {
        return $this->data;
    }
}